package com.lxm.job;

import com.alibaba.fastjson.JSONObject;
import com.lxm.util.DateUtils;
import com.lxm.util.HttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class getWxToken {
    @Value("${wx.gzhAppid}")
    private String wxAppid;
    @Value("${wx.gzhSecret}")
    private String wxSecret;
    @Resource
    private RedisTemplate<String, String> auctionRedisTemplate;

    private String Url="https://api.weixin.qq.com/cgi-bin/token";

    //定时任务启动时候先执行一次
    @PostConstruct
    @Scheduled(cron="0 0/58 * * * ?")
    public void WxToken(){
       JSONObject query =new JSONObject();
        System.out.println(wxAppid);
        System.out.println(wxSecret);
        query.put("grant_type","client_credential");
        query.put("appid",wxAppid);
        query.put("secret",wxSecret);
        JSONObject post = HttpUtils.get(Url, query);
        System.out.println(post);
        String access_token = post.getString("access_token");
        ValueOperations<String, String> stringStringValueOperations = auctionRedisTemplate.opsForValue();
        stringStringValueOperations.set("token",access_token);
    }
}
